﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 设置标题在左还是在右命令。只对SubRoot有效。“SubRoot”指根标题的直接下级。
    /// ★①，修改类名。
    /// </summary>
    public static class SwitchDirectionCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static SwitchDirectionCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "SwitchDirectionCommand",
                "SwitchDirectionCommand",
                typeof(SwitchDirectionCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.L,ModifierKeys.Control,"Ctrl+L"),
                    new KeyGesture(Key.R,ModifierKeys.Control,"Ctrl+R")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            SwitchTitleDirection();
        }

        /// <summary>
        /// 此方法是调用DeleteCommand的Delete来实现。
        /// </summary>
        internal static void SwitchTitleDirection()
        {
            if (Globals.MainWindow.MainSelectedTitle == null)
            {
                MessageBox.Show("　　请选中一个标题再执行。",
                    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Title rootTitle = Globals.MainWindow.RootTitle;
            Title curTitle = Globals.MainWindow.MainSelectedTitle;

            if (curTitle == rootTitle)
            {
                MessageBox.Show("　　根标题（中心标题）不能切换左右方向。",
                    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (curTitle.Editor != null && curTitle.Editor.View != ViewMode.Default)
            {
                MessageBox.Show("　　当前视图不是【思维导图】模式，此操作无意义。",
                    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Title subRootTitle = curTitle.GetSubRoot();

            if (subRootTitle == null)
            {
                MessageBox.Show("　　发生意外，未找到此分支所在一级标题。",
                   Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            ModifingItem mi = new ModifingItem()
            {
                IsNeedRefreshTitleAndLinesLocation = true,
                ModifingName = "左右平移[一级标题]"
            };

            switch (subRootTitle.Direction)
            {
                case TitleDirection.Left:
                    {
                        Action act = new Action(subRootTitle.ID, -1, subRootTitle.GetType().Name,
                            "Direction", TitleDirection.Left.ToString(),
                            TitleDirection.Right.ToString());

                        subRootTitle.Direction = TitleDirection.Right;

                        mi.Add(act);
                        break;
                    }
                case TitleDirection.Right:
                    {
                        Action act = new Action(subRootTitle.ID, -1, subRootTitle.GetType().Name,
                            "Direction", TitleDirection.Right.ToString(),
                            TitleDirection.Left.ToString());

                        subRootTitle.Direction = TitleDirection.Left;

                        mi.Add(act);
                        break;
                    }
            }

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.RefreshTitleAndLinesLocation();

            Globals.MainWindow.TryScrollTitle(curTitle);
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            if (Globals.MainWindow.MainSelectedTitle == null)
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
        }
    }
}
